/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wallythewatermelon;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author mgohde
 */
public class Sprite
{
    public int x, y;
    public int dx=0, dy=0, ddx=0, ddy=0;
    int timecounter=0;
    int localTimeFrame=-1;
    boolean isDead=false;
    int height, width; //This will give dimensions for collision detection.
    Image img;
    
    char blockType;
    
    boolean isAnimated=false;
    Image animationFrames[];
    int framesPerAnimationFrame=1;
    int frameCounter;
    private int curAnimStep;
    
    public void loadAnimation(String[] animationFilesList)
    {
        int i;
        animationFrames=new Image[animationFilesList.length];
        
        for(i=0;i<animationFilesList.length;i++)
        {
            try {
                animationFrames[i]=ImageIO.read(new File("textures/" + animationFilesList[i] + ".png"));
            } catch (IOException ex) {
                System.err.println("ERR: COULD NOT FIND FILE: "+animationFilesList[i]);
            }
        }
    }
    
    public Sprite()
    {
        this.width=64;
        this.height=64;
    }
    
    public Sprite(int newX, int newY, Image imgIn)
    {
        x=newX;
        y=newY;
        img=imgIn;
        this.height =64;
        this.width =64;
    }
    
    public void enableAnimation(int newFramesPerAnimationFrame) throws NoAnimationsException
    {
        if(animationFrames==null)
        {
            throw new NoAnimationsException("ERROR: NO ANIMATIONS LOADED");
        }
        frameCounter=0;
        curAnimStep=0;
        isAnimated=true;
        framesPerAnimationFrame=newFramesPerAnimationFrame;
    }
    
    public Sprite(int x, int y, String imgName) {
        this.x = x;
        this.y = y;
        this.loadGraphics(imgName);
        this.height = img.getHeight(null);
        this.width = img.getWidth(null);
    }
    
    /**
     * 
     * @param path = name of image in textures folder
     */
    public final void loadGraphics(String path)
    {
        try {
            img=ImageIO.read(new File("textures/" + path + ".png"));
        } catch (IOException ex) {
            System.err.println("ERR: COULD NOT LOAD GRAPHICS FILE "+path);
        }
    }
    
    public void draw(Graphics g) 
    {
        if(!isDead&&!isAnimated)
        {
            g.drawImage(img, x, y, null);
        }
        
        else if(!isDead&&isAnimated)
        {
            g.drawImage(animationFrames[curAnimStep], x, y, null);
            frameCounter++;
            
            if(frameCounter==framesPerAnimationFrame)
            {
                frameCounter=0;
                curAnimStep++;
            }
            
            if(curAnimStep>=animationFrames.length)
            {
                curAnimStep=0;
            }
        }
        
        x+=dx;
        y+=dy;
        
        dx+=ddx;
        dy+=ddy;
    }
    
    public void setPosition(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setTrajectory(int newDx, int newDy)
    {
        dx=newDx;
        dy=newDy;
    }
    
    public void setXTrajectory(int newDx) {
        dx= newDx;
    }
    
    public boolean isCollidingWith(Sprite otherSprite)
    {
        boolean collisionDetected=true;
        int leftSideHere, leftSideThere;
        int rightSideHere, rightSideThere;
        int topSideHere, topSideThere;
        int bottomSideHere, bottomSideThere;

        //Calc for left sides.
        leftSideHere=x;
        leftSideThere=otherSprite.x;
        //Calc for right sides.
        rightSideHere=leftSideHere+width;
        rightSideThere=leftSideThere+otherSprite.width;
        //Calc for top sides;
        topSideHere=y;
        topSideThere=otherSprite.y;
        
        bottomSideHere=y+height;
        bottomSideThere=otherSprite.height;
        
        if(bottomSideHere<=topSideThere)
        {
            collisionDetected=false;
        }
        
        if(topSideHere>=bottomSideThere)
        {
            collisionDetected=false;
        }
        
        if(rightSideHere<=leftSideThere)
        {
            collisionDetected=false;
        }
        
        if(leftSideHere>=rightSideThere)
        {
            collisionDetected=false;
        }
        
        return collisionDetected;
    }
}
